package com.longrise.android.byjk.plugins.tabfirst;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.model.HomeMultipleItem;
import com.longrise.common.utils.AppUtil;
import com.longrise.android.byjk.utils.Tracker_Google;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by callxkj on 2017/2/23.
 * 首页主体的recycle的adapter
 */

public class HomeRcvAdapter extends BaseMultiItemQuickAdapter<HomeMultipleItem, BaseViewHolder> {


    private List<EntityBean> mListBeans = new ArrayList<>();
    private onItemTypeClickListener mTypeClickListener;


    public void setData(List<HomeMultipleItem> data, EntityBean[] beans) {
        mListBeans.clear();
        for (EntityBean bean : beans) {
            mListBeans.add(bean);
        }
        setNewData(data);
    }

    /**
     * 设置 新手专区 保险头条里面的image的点击事件
     */
    public void setOnItemTypeClickListener(onItemTypeClickListener listener) {
        this.mTypeClickListener = listener;
    }

    public HomeRcvAdapter(List<HomeMultipleItem> data) {
        super(data);
        addItemType(HomeMultipleItem.TYPE1, R.layout.homefragment_item1);//新手专区样式
        addItemType(HomeMultipleItem.TYPE2, R.layout.homefragment_item2);//保险头条样式
        addItemType(HomeMultipleItem.TYPE3, R.layout.homefragment_item3);//横排有一个图片的样式
        addItemType(HomeMultipleItem.TYPE4, R.layout.homefragment_item4);//横排两个下面一个一共三个图片的样式
    }

    @Override
    protected void convert(BaseViewHolder helper, HomeMultipleItem item) {
        int adapterPosition = helper.getAdapterPosition();
        EntityBean entityBean = mListBeans.get(adapterPosition - 1);
        View convertView = helper.getConvertView();
        String areaname = entityBean.getString("areaname");
        EntityBean[] childmenus = entityBean.getBeans("childmenus");
        String loadmoretype = entityBean.getString("loadmoretype");
        String style = entityBean.getString("style");
        switch (helper.getItemViewType()) {
            case HomeMultipleItem.TYPE1: //新手专区样式的item
                parseType1(childmenus, convertView, areaname);
                break;
            case HomeMultipleItem.TYPE2: //保险头条样式的item 带有字体
                parseType2(childmenus, convertView, areaname, loadmoretype);
                break;
            case HomeMultipleItem.TYPE3: //横排有两个图片的样式 宝易工具(一个区域)
            case HomeMultipleItem.TYPE4: //横排两个下面一个一共三个图片的样式 宝易工具(三个区域)
            case HomeMultipleItem.TYPE5: // 宝易工具(五个区域)
                parseType345(childmenus, convertView, style,item);
                break;
        }
    }

    /**
     * 处理type1(新手专区) 数据
     */
    private void parseType1(EntityBean[] childmenuses, View convertView, String areaname) {

        RecyclerView recyclerView = (RecyclerView) convertView.findViewById(R.id.homefragment_item1_rcv);
        TextView mTvTitle = (TextView) convertView.findViewById(R.id.homefragment_item1_title);
        mTvTitle.setText(areaname);
        HomeRcvItem1Adapter adapter = new HomeRcvItem1Adapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);
        adapter.setOnItem1ClickListener(new HomeRcvItem1Adapter.OnItem1ClickListener() {
            @Override
            public void onClick(String url, String title, String linkedtype) {
                if (mTypeClickListener != null) {
                    mTypeClickListener.onClick(HomeMultipleItem.TYPE1, url, title, linkedtype, null);
                }
            }
        });

        View view = View.inflate(AppUtil.getContext(), R.layout.item_homercvitem_footview, null);
        adapter.addFooterView(view, 0, LinearLayout.HORIZONTAL);
        adapter.refreshData(childmenuses);
    }

    /**
     * 处理type2(保宝头条) 数据
     */
    private void parseType2(EntityBean[] childmenuses, View convertView, String areaname, String loadmoretype) {

        RecyclerView recyclerView2 = (RecyclerView) convertView.findViewById(R.id.homefragment_item2_rcv);
        TextView mTvTitle = (TextView) convertView.findViewById(R.id.homefragment_item2_title);
        RelativeLayout mRlrightTitle = (RelativeLayout) convertView.findViewById(R.id.homefragment_item2_more_rl);
        mTvTitle.setText(areaname);
        //1001 表示有查看全部按钮,即显示查看全部
        if ("1001".equals(loadmoretype)) {
            mRlrightTitle.setVisibility(View.VISIBLE);
            mRlrightTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mTypeClickListener != null) {
                        mTypeClickListener.onClick(HomeMultipleItem.TYPE2_1, null, null, null, null);
                    }
                    Tracker_Google.getInstance().addEvent("BB-headlines", "View-more", "查看更多");
                }
            });
        } else {
            mRlrightTitle.setVisibility(View.GONE);
            mRlrightTitle.setOnClickListener(null);
        }

        HomeRcvItem2Adapter adapter2 = new HomeRcvItem2Adapter();
        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(mContext);
        linearLayoutManager2.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView2.setLayoutManager(linearLayoutManager2);
        recyclerView2.setAdapter(adapter2);
        adapter2.setOnItem2ClickListener(new HomeRcvItem2Adapter.OnItem2ClickListener() {
            @Override
            public void onClick(String url, String title, String linkedtype) {
                if (mTypeClickListener != null) {
                    mTypeClickListener.onClick(HomeMultipleItem.TYPE2, url, title, linkedtype, null);
                }
            }
        });

        View view = View.inflate(AppUtil.getContext(), R.layout.item_homercvitem_footview, null);
        adapter2.addFooterView(view, 0, LinearLayout.HORIZONTAL);
        adapter2.refreshData(childmenuses);
    }

    /**
     * 宝易3种 集中处理
     */
    private void parseType345(EntityBean[] childmenus, View convertView, final String style,
                              HomeMultipleItem item) {
        //动态设置区域高度
        int screenwidth = AppUtil.getScreenWidth();
        int itemType = item.getItemType();
        switch (itemType){
            case HomeMultipleItem.TYPE4:
                LinearLayout ll = (LinearLayout) convertView.findViewById(R.id.home_fragment_item4_ll1);
                ImageView iv = (ImageView) convertView.findViewById(R.id.home_fragment_item4_iv3);
                int llHeight = (int) (((float)120/375)*screenwidth);
                ViewGroup.LayoutParams layoutParamsll = ll.getLayoutParams();
                layoutParamsll.height = llHeight;
                ll.setLayoutParams(layoutParamsll);
                int ivHeight = (int) (((float)150/375)*screenwidth);
                ViewGroup.LayoutParams layoutParamsiv = iv.getLayoutParams();
                layoutParamsiv.height = ivHeight;
                iv.setLayoutParams(layoutParamsiv);
                break;
        }

        for (int x = 0; x < childmenus.length; x++) {
            final String uniqueid = childmenus[x].getString("uniqueid");
            String picpath = childmenus[x].getString("picpath");
            final String linkedtype = childmenus[x].getString("linkedtype");
            String linkedurl = childmenus[x].getString("linkedurl");
            final String text = childmenus[x].getString("text");
            switch (x) {
                case 0:
                    ImageView iv1 = (ImageView) convertView.findViewById(R.id.home_fragment_item4_iv1);
                    Glide.with(AppUtil.getContext()).load(picpath).into(iv1);
                    iv1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if ("1005".equals(style) && mTypeClickListener != null) {
                                mTypeClickListener.onClick(HomeMultipleItem.TYPE4,null,text,
                                        linkedtype,uniqueid);
                            }
                        }
                    });
                    break;
                case 1:
                    ImageView iv2 = (ImageView) convertView.findViewById(R.id.home_fragment_item4_iv2);
                    Glide.with(AppUtil.getContext()).load(picpath).into(iv2);
                    iv2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if ("1005".equals(style) && mTypeClickListener != null) {
                                mTypeClickListener.onClick(HomeMultipleItem.TYPE4,null,text,
                                        linkedtype,uniqueid);
                            }
                        }
                    });
                    break;
                case 2:
                    ImageView iv3 = (ImageView) convertView.findViewById(R.id.home_fragment_item4_iv3);
                    Glide.with(AppUtil.getContext()).load(picpath).into(iv3);
                    iv3.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if ("1005".equals(style) && mTypeClickListener != null) {
                                mTypeClickListener.onClick(HomeMultipleItem.TYPE4,null,text,
                                        linkedtype,uniqueid);
                            }
                        }
                    });
                    break;
            }
        }
    }


    interface onItemTypeClickListener {
        void onClick(int type, String url, String text, String linkedtype, String uniqueid);
    }
}
